<?php

class Nuo_Html_Element_Group extends Nuo_Html_Element
{
	/**
	 * @var array
	 */
	protected $_meta = array(
		'tag' => 'div',
		'render' => 'group',
	);

	/**
	 * @return array
	 */
	public function getValue()
	{
		$data = array();

		foreach ($this->_children as $name => $element)
		{
			$data[$name] = $element -> getValue();
		}

		return $data;
	}

	/**
	 * set value
	 * @param array $data
	 * @return Nuo_Html_Element
	 */
	public function setValue($data)
	{
		foreach ($this->_children as $name => $element)
		{
			$element -> setValue(isset($data[$name]) ? $data[$name] : null);
		}
		return $this;
	}

	/**
	 * @param string $type
	 * @param string $name
	 * @param array $options
	 * @return Nuo_Html_Element
	 */
	public function addChild($type, $name, $options = array())
	{
		$class = 'Nuo_Html_Element_' . ucfirst($type);

		$this -> _children[$name] = $ele = new $class;

		$ele -> setOptions($options);
		
		if(!$this instanceof Nuo_Html_Element_Form)
		{
			$name = $this -> getName() . '[' . $name . ']';
		}

		$ele -> setParent($this) -> setName($name);

		return $this;
	}

	public function isValid($data)
	{
		$flag = true;

		foreach ($this->_children as $name => $element)
		{
			if (!$element -> isValid(isset($data[$name]) ? $data[$name] : null))
			{
				$flag = false;
			}
		}
		return $flag;
	}

}
